package com.wjerp.tplus.server.common.security;

import com.wjerp.tplus.server.domain.entity.sys.PermissionActionRelation;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.util.CollectionUtils;

import java.util.*;


/**
 * @author lishuailei
 */
public class PermissionActionMetaData {

	private static Map<String, Collection<ConfigAttribute>> metaData;
	
	public static Map<String, Collection<ConfigAttribute>> getMetaData(){
		return metaData;
	}
	
	public static void setMetaData(List<PermissionActionRelation> list){
		Map<String, Collection<ConfigAttribute>> tmpMetaData = new HashMap<String, Collection<ConfigAttribute>>();
		if(!CollectionUtils.isEmpty(list)){
			for(PermissionActionRelation itr : list){
				ConfigAttribute ca = new SecurityConfig(itr.getPermission());
				if(tmpMetaData.containsKey(itr.getActionUrl())){
					tmpMetaData.get(itr.getActionUrl()).add(ca);
				}else{
					Set<ConfigAttribute> permissionSet = new HashSet<ConfigAttribute>();
					permissionSet.add(ca);
					tmpMetaData.put(itr.getActionUrl(), permissionSet);
				}
			}
		}
		metaData = tmpMetaData;
	}
}
